#ifndef BULLETCOLLISIONALGORITHM_H
#define BULLETCOLLISIONALGORITHM_H

#include "BulletCollision/CollisionDispatch/btActivatingCollisionAlgorithm.h"
#include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
#include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"

class btPersistentManifold;

/// BulletCollisionAlgorithm  provides sphere-sphere collision detection.
/// Other features are frame-coherency (persistent data) and collision response.
/// Also provides the most basic sample for custom/user btCollisionAlgorithm
class BulletCollisionAlgorithm : public btActivatingCollisionAlgorithm
{
    bool	m_ownManifold;
    btPersistentManifold*	m_manifoldPtr;

public:
    BulletCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* body0,btCollisionObject* body1);

    BulletCollisionAlgorithm(const btCollisionAlgorithmConstructionInfo& ci)
        : btActivatingCollisionAlgorithm(ci) {}

    virtual void processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);

    virtual btScalar calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);

    virtual	void	getAllContactManifolds(btManifoldArray&	manifoldArray)
    {
        if (m_manifoldPtr && m_ownManifold)
        {
            manifoldArray.push_back(m_manifoldPtr);
        }
    }

    virtual ~BulletCollisionAlgorithm();

    struct CreateFunc :public 	btCollisionAlgorithmCreateFunc
    {
        virtual	btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1)
        {
            void* mem = ci.m_dispatcher1->allocateCollisionAlgorithm(sizeof(BulletCollisionAlgorithm));
            return new(mem) BulletCollisionAlgorithm(0,ci,body0,body1);
        }
    };

};

#endif // BULLETCOLLISIONALGORITHM_H
